import { get as _get } from 'object-path';
import {
    getNewsList,
    getNewsDetail,
    getNewsSort,
    getNewsHide,
    getAddNews,
    getEditNews,
    getNewsType
} from '@/services/setting/newsManage';

export default {
    namespace: 'newsManage',
    state: {
        data: [],
        paginationNews: {
            pageSize: 10,
            current: 1,
            total: 0
        },
        detailData: {
            applicationNo: '',
            createTime: '',
            createUser: '',
            hidden: 1,
            id: null,
            newsCnContent: '',
            newsCnName: '',
            newsCode: '',
            newsEnContent: '',
            newsEnName: '',
            releaseTime: '',
            resourceRelation: [],
            topping: 0,
            labelCn: [],
            labelEn: [],
            newsTypes: '唉唉唉'
        },
        newsType: []
    },
    effects: {
        // eslint-disable-next-line no-unused-vars
        *fetchNewsList({ payload }, { put, call }) {
            const result = yield call(getNewsList, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        data: _get(result, 'data.rows', []),
                        paginationNews: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
        },
        *fetchNewsDetail({ payload }, { put, call }) {
            const result = yield call(getNewsDetail, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        detailData: _get(result, 'data', {})
                    }
                });
            }
        },
        *fetchNewsSort({ payload, callback }, { call }) {
            const result = yield call(getNewsSort, payload);
            if (callback) callback(result);
        },
        *fetchNewsHide({ payload, callback }, { call }) {
            const result = yield call(getNewsHide, payload);
            if (callback) callback(result);
        },
        *fetchAddNews({ payload, callback }, { call }) {
            const result = yield call(getAddNews, payload);
            if (callback) callback(result);
            // if (result && result.code === 10000) {
            //     yield put({
            //         type: 'save',
            //         payload: {
            //             addNews: _get(result, 'data', {})
            //         }
            //     });
            // }
        },
        *fetchEditNews({ payload, callback }, { call }) {
            const result = yield call(getEditNews, payload);
            // if (result && result.code === 10000) {
            //     yield put({
            //         type: 'save',
            //         payload: {
            //             editNews: _get(result, 'data', {})
            //         }
            //     });
            // }
            if (callback) callback(result);
        },
        *fetchNewsType({ payload, callback }, { call, put }) {
            const result = yield call(getNewsType, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        newsType: _get(result, 'data', [])
                    }
                });
            }
            if (callback) callback(result);
        }

    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        clear(state) {
            return {
                ...state,
                detailData: {
                    applicationNo: '',
                    createTime: '',
                    createUser: '',
                    hidden: 1,
                    id: null,
                    newsCnContent: '',
                    newsCnName: '',
                    newsCode: '',
                    newsEnContent: '',
                    newsEnName: '',
                    releaseTime: '',
                    resourceRelation: [],
                    topping: 0,
                    labelCn: [],
                    labelEn: [],
                    newsTypes: []
                }
            };
        }
    }
};
